"""
文本工具
"""
import re
import jieba
from typing import List, Dict, Any


def clean_text(text: str) -> str:
    """清理文本"""
    if not text:
        return ""
    
    # 移除多余的空白字符
    text = re.sub(r'\s+', ' ', text.strip())
    
    # 移除特殊字符
    text = re.sub(r'[^\w\s\u4e00-\u9fff]', '', text)
    
    return text


def split_sentences(text: str) -> List[str]:
    """分割句子"""
    if not text:
        return []
    
    # 中文分句
    sentence_pattern = r'[。！？；：]'
    sentences = re.split(sentence_pattern, text)
    
    # 过滤空句子
    sentences = [s.strip() for s in sentences if s.strip()]
    
    return sentences


def extract_keywords(text: str, top_k: int = 10) -> List[str]:
    """提取关键词"""
    if not text:
        return []
    
    # 使用jieba提取关键词
    keywords = jieba.analyse.extract_tags(text, topK=top_k)
    
    return keywords


def calculate_text_similarity(text1: str, text2: str) -> float:
    """计算文本相似度（基于词重叠）"""
    if not text1 or not text2:
        return 0.0
    
    # 分词
    words1 = set(jieba.lcut(text1))
    words2 = set(jieba.lcut(text2))
    
    # 计算Jaccard相似度
    intersection = len(words1.intersection(words2))
    union = len(words1.union(words2))
    
    if union == 0:
        return 0.0
    
    return intersection / union


def normalize_text(text: str) -> str:
    """标准化文本"""
    if not text:
        return ""
    
    # 转换为小写
    text = text.lower()
    
    # 移除标点符号
    text = re.sub(r'[^\w\s]', '', text)
    
    # 移除多余空格
    text = re.sub(r'\s+', ' ', text.strip())
    
    return text


def extract_title(text: str) -> str:
    """提取标题"""
    if not text:
        return ""
    
    # 取第一行作为标题
    lines = text.split('\n')
    for line in lines:
        line = line.strip()
        if line:
            return line[:100]  # 限制长度
    
    return ""


def count_words(text: str) -> int:
    """统计词数"""
    if not text:
        return 0
    
    # 使用jieba分词统计
    words = jieba.lcut(text)
    return len(words)


def count_characters(text: str) -> int:
    """统计字符数"""
    if not text:
        return 0
    
    return len(text)


def get_text_statistics(text: str) -> Dict[str, Any]:
    """获取文本统计信息"""
    if not text:
        return {
            "char_count": 0,
            "word_count": 0,
            "sentence_count": 0,
            "line_count": 0,
        }
    
    return {
        "char_count": count_characters(text),
        "word_count": count_words(text),
        "sentence_count": len(split_sentences(text)),
        "line_count": len(text.split('\n')),
    }


def remove_html_tags(text: str) -> str:
    """移除HTML标签"""
    if not text:
        return ""
    
    # 移除HTML标签
    text = re.sub(r'<[^>]+>', '', text)
    
    # 移除HTML实体
    text = re.sub(r'&[a-zA-Z]+;', '', text)
    
    return text


def extract_urls(text: str) -> List[str]:
    """提取URL"""
    if not text:
        return []
    
    # URL正则表达式
    url_pattern = r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
    
    urls = re.findall(url_pattern, text)
    return urls


def extract_emails(text: str) -> List[str]:
    """提取邮箱地址"""
    if not text:
        return []
    
    # 邮箱正则表达式
    email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
    
    emails = re.findall(email_pattern, text)
    return emails


def is_chinese_text(text: str) -> bool:
    """判断是否为中文文本"""
    if not text:
        return False
    
    # 检查是否包含中文字符
    chinese_pattern = r'[\u4e00-\u9fff]'
    return bool(re.search(chinese_pattern, text))


def get_text_language(text: str) -> str:
    """检测文本语言"""
    if not text:
        return "unknown"
    
    # 简单的语言检测
    chinese_chars = len(re.findall(r'[\u4e00-\u9fff]', text))
    english_chars = len(re.findall(r'[a-zA-Z]', text))
    
    if chinese_chars > english_chars:
        return "chinese"
    elif english_chars > chinese_chars:
        return "english"
    else:
        return "mixed" 